<template>
    <div class="statistics_box">
        <div class="title red-title">红包趋势</div>
        <div class="border"></div>
        <div class="btn_box">
            <el-radio-group v-model="pageType" @change="redChange">
                <el-radio-button :label="0">发放个数</el-radio-button>
                <el-radio-button :label="1">发放金额</el-radio-button>
            </el-radio-group>
            <el-button
                v-if="chartData.date.length"
                @click="export2Img(myChartRedRef, pageType ? '发放金额' : '发放个数')"
                >导出</el-button
            >
        </div>
        <div v-if="chartData.date.length" id="echarts" ref="myChartRedRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </div>
    <div class="statistics_box mb0">
        <div class="title customer-title" style="align-items: center; padding-bottom: 8px">
            客户新增趋势
            <el-button v-if="chartData.date.length" @click="export2Img(myChartCustAddRef, '客户新增趋势')"
                >导出</el-button
            >
        </div>
        <div class="border"></div>
        <div class="btn_box" style="justify-content: flex-end"></div>
        <div v-if="chartData.date.length" id="echarts" ref="myChartCustAddRef"></div>
        <div v-else class="none_data">
            <img class="no_img" src="@/assets/noData.png" alt="" />
            <div>暂无数据</div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, inject, defineExpose, nextTick } from 'vue';
import { export2Img } from '@/utils/utils';
const chartData = ref({
    date: [],
    red_num: [],
    red_amount: [],
    cust_num: []
});
const echarts: any = inject('ec');
const pageType = ref(0);
const myChartRedRef = ref(null);
const myChartCustAddRef = ref(null);
const getEChartRed = () => {
    if (chartData.value.date.length) {
        const myChartLine = echarts?.init(myChartRedRef.value);
        const option = {
            color: ['#fa6c00'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#FF6B00'
                    }
                }
            },
            grid: {
                top: 60,
                bottom: 30,
                left: 32,
                right: 40,
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: chartData.value.date,
                boundaryGap: false,
                // axisTick: {
                //   show: false,
                //   alignWithLabel: true
                // },
                offset: 3
            },
            yAxis: {
                type: 'value',
                name: pageType.value ? '￥' : '个',
                nameGap: 10, // y轴name与横纵坐标轴线的间距
                nameTextStyle: {
                    padding: [0, 35, 10, 0] // 加上padding可以调整其位置
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#969696'
                },
                splitLine: {
                    lineStyle: {
                        type: 'line',
                        color: '#e8e8e8' // 分割线颜色
                    }
                }
            },
            series: [
                {
                    name: pageType.value ? '￥' : '个',
                    type: 'line',
                    data: pageType.value ? chartData.value.red_amount : chartData.value.red_num,
                    smooth: true,
                    symbol: 'circle',
                    symbolSize: 0,
                    areaStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgba(247, 222, 211)' },
                                { offset: 0.5, color: 'rgba(251, 230, 223)' },
                                { offset: 1, color: 'rgba(255, 255, 255)' }
                            ])
                        }
                    },
                    itemStyle: {
                        normal: {
                            borderWidth: 2,
                            color: '#FF9D23'
                        }
                    },
                    emphasis: {
                        // 鼠标 hover 时的样式
                        itemStyle: {
                            color: '#FF9D23', // hover 圆点颜色
                            borderColor: '#FF9D23', // hover 圆点边框颜色
                            borderWidth: 8
                        }
                    }
                }
            ]
        };
        myChartLine.setOption(option);
        nextTick(() => {
            myChartLine.resize();
        });
        window.onresize = function () {
            myChartLine.resize();
        };
    }
};
const getEChartCustAdd = () => {
    if (chartData.value.date.length) {
        const myChartLine = echarts?.init(myChartCustAddRef.value);
        const option = {
            color: ['#fa6c00'],
            tooltip: {
                trigger: 'axis',
                axisPointer: {
                    type: 'cross',
                    label: {
                        backgroundColor: '#FF6B00'
                    }
                }
            },
            grid: {
                top: 40,
                bottom: 30,
                left: 32,
                right: 40,
                containLabel: true
            },
            xAxis: {
                type: 'category',
                data: chartData.value.date,
                boundaryGap: false,
                // axisTick: {
                //   show: false,
                //   alignWithLabel: true
                // },
                offset: 3
            },
            yAxis: {
                type: 'value',
                name: '人',
                nameGap: 10, // y轴name与横纵坐标轴线的间距
                nameTextStyle: {
                    padding: [0, 35, 10, 0] // 加上padding可以调整其位置
                },
                axisTick: {
                    show: false
                },
                axisLabel: {
                    color: '#969696'
                },
                splitLine: {
                    lineStyle: {
                        type: 'line',
                        color: '#e8e8e8' // 分割线颜色
                    }
                }
            },
            series: [
                {
                    name: '人',
                    type: 'line',
                    data: pageType.value ? chartData.value.red_amount : chartData.value.cust_num,
                    symbol: 'circle',
                    symbolSize: 0,
                    smooth: true,
                    areaStyle: {
                        normal: {
                            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
                                { offset: 0, color: 'rgba(247, 222, 211)' },
                                { offset: 0.5, color: 'rgba(251, 230, 223)' },
                                { offset: 1, color: 'rgba(255, 255, 255)' }
                            ])
                        }
                    },
                    itemStyle: {
                        normal: {
                            borderWidth: 2,
                            color: '#FF9D23'
                        }
                    },
                    emphasis: {
                        // 鼠标 hover 时的样式
                        itemStyle: {
                            color: '#FF9D23', // hover 圆点颜色
                            borderColor: '#FF9D23', // hover 圆点边框颜色
                            borderWidth: 8
                        }
                    }
                }
            ]
        };
        myChartLine.setOption(option);
        nextTick(() => {
            myChartLine.resize();
        });
        window.onresize = function () {
            myChartLine.resize();
        };
    }
};
const redChange = () => {
    getEChartRed();
};
const getData = (val: any) => {
    chartData.value = val;
    nextTick(() => {
        getEChartRed();
        getEChartCustAdd();
    });
};
defineExpose({
    getData
});
</script>

<style lang="scss" scoped>
.statistics_box {
    padding: 8px 26px;
    margin-bottom: 16px;
    background: #fff;
    box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
    border: 1px solid #eaeaea;
    font-size: 14px;
    color: #33302d;

    .title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 20px;
        border-bottom: 1px solid #eaeaea;
        font-size: 16px;
    }

    .red-title {
        padding-top: 20px;
    }

    .customer-title {
        line-height: 40px;
    }

    .btn_box {
        display: flex;
        justify-content: space-between;
        padding: 16px 0 0;
    }

    #echarts {
        display: flex;
        justify-content: center;
        height: 320px;
    }
}

.mb0 {
    margin-bottom: 0;
}

.circle_box {
    display: flex;
    margin-bottom: 16px;

    .circle {
        flex: 1;
        padding: 20px 26px;
        margin-right: 20px;
        background: #fff;
        box-shadow: 0 4px 4px 0 rgb(240 240 240 / 50%);
        border: 1px solid #eaeaea;

        .circle_title {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding-bottom: 8px;
            border-bottom: 1px solid #eaeaea;
        }
    }

    .margin {
        margin-right: 0;
    }
}

.none_data {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    color: #909399;

    img {
        transform: translateX(15%);
    }
}
</style>
